﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Netfraction.Network.Protocol.BASE.Handler
{
    public class HandlerSND : ProtocolMessageActionHandler
    {
        public HandlerSND()
            : base()
        {
            mSupportedConnectionStates = new List<RemoteMachine.ConnectionState>();
        }

        #region ProtocolMessageActionHandler Members

        public override string ProtocolActionCode
        {
            get { return "SND"; }
        }

        // this handler is way to long. break it into smaller functions
        public override bool HandleProtocolMessage(Netfraction.RemoteMachine.Node node, ProtocolMessage message, IEnumerable<ProtocolFeature> extenders)
        {
            if (node is RemoteMachine.Peer) // The Hub is in this Peer list I believe. 
                                            // Since extensions can enable hub-client array transfer, perhaps something should cater for that here?
            {
                node.Connection.State = RemoteMachine.ConnectionState.Data;
                
                System.Diagnostics.Debug.WriteLine("in SND handler");
                if (message.Parameters.Count >= 4)  // allow extension parameters. but must have at least 4.
                {
                    System.Diagnostics.Debug.WriteLine(String.Format("Type: {0}, Identifier: {1}, Start Position: {2}, Bytes: {3}", 
                        message.Parameters[0].ParameterValue,
                        message.Parameters[1].ParameterValue,
                        message.Parameters[2].ParameterValue,
                        message.Parameters[3].ParameterValue));

                    // all cases will send some variety of Stream, be it a FileStream or MemoryStream

                    string type = message.Parameters[0].ParameterValue;
                    // "Extensions may add to the identifier names as well as add new types."
                    // the TIGR feature adds support for "tthl" too
                    string identifier = message.Parameters[1].ParameterValue;

                    long bytesExpected = Convert.ToInt64(message.Parameters[3].ParameterValue);

                    //System.IO.MemoryStream mStream = null;
                    switch (type)
                    {
                        case "file":
                            break;
                        case "list": // partial file lists
                            break;
                        default: // unrecognised or extension-implemented types
                            break;
                    }
                    string tempPath = /*@"C:\Users\user\Desktop\Download.dat";//*/System.IO.Path.GetTempFileName();
                    System.Diagnostics.Debug.WriteLine(tempPath);

                    (node.Connection as RemoteMachine.PeerConnection).expectedBytes = bytesExpected;
                    (node.Connection as RemoteMachine.PeerConnection).fileStream = new System.IO.FileStream(tempPath,
                        System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.Read);

                }
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion

    }
}
